package com.tgy.leetcode;

/**
 * @Author: tgy
 * @Date: 12/22/20 11:27 AM
 *
 *  https://leetcode-cn.com/problems/powx-n/
 */
public class _50_Pow_01 {

    public double myPow(double x, int n) {

        boolean flagOut = n < 0;
        long m = flagOut ? -((long)n):n;

        double result = 1.0;
        double tmp = x;

        while (m > 0) {

            // 判断每一位是否为 1
            boolean flag = ((m & 1) == 1);

            if (flag) {

                result *=tmp;
            }

            tmp *=tmp;
            m>>=1;
        }

        return flagOut?1/result:result;
    }
}
